Imports Col = System.Collections
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Sys = System
Imports Tex = System.Text
Imports W3x = System.Xml

Namespace Enhancements.Core.Logging
    ''' <summary>A simple procedure passage line style formatter. It implements limited formatting features.</summary>
    Public NotInheritable Class SingleLineFormatter
        Inherits Formatter

        Private __StringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)

        ''' <summary>Initializes a SingleLineFormatter instance.</summary>
        ''' <param name="_configuration">Configuration to use by the instance.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_configuration is null.</exception>
        ''' <exception cref="IsOfTypeNotException">_configuration is not of type SingleLineFormatterConfiguration.</exception>
        Public Sub New(ByVal _configuration As FormatterConfiguration)
            Configuration = _configuration
        End Sub

        ''' <summary>Gets and sets the configuration used by the instance.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">The value is null (while setting).</exception>
        ''' <exception cref="IsOfTypeNotException">The value is not of type SingleLineFormatterConfiguration (while setting).</exception>
        Public Overrides Property Configuration() As FormatterConfiguration
            Get
                ___Assert.___IsDisposedNot("SingleLineFormatter[instance]", ___DisposeCalled, True)

                Return CType(__Configuration, SingleLineFormatterConfiguration)
            End Get
            Set(ByVal _value As FormatterConfiguration)
                ___Assert.___IsDisposedNot("SingleLineFormatter[instance]", ___DisposeCalled, True)
                ___Assert.___IsNullNot("Configuration._value", _value, True)
                ___Assert.___IsOfType("Configuration._value", _value, GetType(SingleLineFormatterConfiguration), True)

                __Configuration = _value
            End Set
        End Property

        Friend Overrides Sub ___Format(ByVal _record As ___Record)
            ___Assert.___IsDisposedNot("SingleLineFormatter[instance]", ___DisposeCalled, True)
            ___Assert.___IsNullNot("_record", _record, True)
            ___Assert.___IsEmptyNot("_record.___UnformattedIdentifier", _record.___UnformattedIdentifier, True)
            ___Assert.___IsNullNot("_record.___UnformattedCallingProcedure", _record.___UnformattedCallingProcedure, True)
            ___Assert.___IsNullNot("_record.___UnformattedObjects", _record.___UnformattedObjects, True)

            __StringBuilder.Remove(0, __StringBuilder.Length)

            Dim _configuration As SingleLineFormatterConfiguration = CType(Configuration, SingleLineFormatterConfiguration)

            __StringBuilder.Append(_record.___UnformattedIdentifier.ToString("N"))

            __StringBuilder.Append(_configuration.SeparationCharacter)

            __StringBuilder.Append(_record.___UnformattedMoment.ToShortDateString())
            __StringBuilder.Append(" "c)
            __StringBuilder.Append(_record.___UnformattedMoment.TimeOfDay.ToString())

            __StringBuilder.Append(_configuration.SeparationCharacter)

            __StringBuilder.Append(_record.___UnformattedLevel.ToString())

            __StringBuilder.Append(_configuration.SeparationCharacter)

            __StringBuilder.Append(_record.___UnformattedCallingProcedure.ReflectedType.FullName)
            __StringBuilder.Append("."c)
            __StringBuilder.Append(_record.___UnformattedCallingProcedure.Name)

            __StringBuilder.Append(_configuration.SeparationCharacter)

            __StringBuilder.Append(_record.___UnformattedProcedureExecutionMoment.ToString())

            __StringBuilder.Append(_configuration.SeparationCharacter)

            For _counter As Sys.Int32 = 0 To _record.___UnformattedObjects.Length - 1
                Dim _object As Sys.Object = _record.___UnformattedObjects(_counter)
                If ___Assert.___IsNull("_object", _object, False) Then
                    __StringBuilder.Append("#null#")
                Else
                    If _object Is GetType(Col.IList) Then
                        Dim _objectIList As Col.IList = CType(_object, Col.IList)
                        __StringBuilder.Append("{")
                        For _counter2 As Sys.Int32 = 0 To _objectIList.Count - 1
                            If ___Assert.___IsNull("_objectIList(_counter2)", _objectIList(_counter2), False) Then
                                __StringBuilder.Append("#null#")
                            Else
                                __StringBuilder.Append(_objectIList(_counter2).ToString())
                            End If
                            If ___Assert.___IsInRangeNot("_counter2", _counter2, _objectIList.Count - 1, _objectIList.Count - 1, False) Then
                                __StringBuilder.Append(_configuration.SeparationCharacter)
                            End If
                        Next _counter2
                        __StringBuilder.Append("}")
                    Else
                        __StringBuilder.Append(_object.ToString())
                    End If
                End If
                __StringBuilder.Append(_configuration.SeparationCharacter)
            Next _counter

            __StringBuilder.Append(vbCrLf)

            _record.___Formatted = CType(Configuration, SingleLineFormatterConfiguration).Encoding.GetBytes(__StringBuilder.ToString())
        End Sub

        Friend Overrides Sub ___Dispose()
            ___Assert.___IsDisposedNot("SingleLineFormatter[instance]", ___DisposeCalled, True)
            ___DisposeCalled = True

            __StringBuilder.Remove(0, __StringBuilder.Length)
        End Sub

    End Class
End Namespace
